* Here the information on how to clean each of the datasets used for figure 2 prior to running the 9 .do files (Figure2a.do, Figure2b.do, ecc.. until Figure 2i.do) - these files replicate the final figures. 

***************************************************************
				***** EUROBAROMETER ***** 
***************************************************************

*used for figures  Figure2a.do, Figure2b.do and Figure2c.do 

* The original dataset has  doi:10.4232/1.13429, it's accessible through GESIS (7575)

*SD2: allows singling out groups

g Minority_sex=0 if sd2_5==0
replace Minority_sex=1 if sd2_5==1

g Minority_disable=0 if sd2_6==0
replace Minority_disable=1 if sd2_6==1

g Minority_religion=0 if sd2_3==0
replace Minority_religion=1 if sd2_3==1

rename d11 Age

*QC1: Perceptions of discrimination

replace qc1_1=. if qc1_1==6
replace qc1_2=. if qc1_2==6
replace qc1_3=. if qc1_3==6
replace qc1_7=. if qc1_7==6
replace qc1_4=. if qc1_4==6
replace qc1_6=. if qc1_6==6
replace qc1_9=. if qc1_9==6
replace qc1_8=. if qc1_8==6
replace qc1_10=. if qc1_10==6

g PD_sex=qc2_4 if Minority_sex==1
g PD_disable=qc2_10 if Minority_disable==1
g PD_religion=qc2_9  if Minority_religion==1
g PD_age=qc2_8 if Age>51
*51 is the overall average age of respondents, so we create old people/young people based on this


*Discriminatory attitudes

replace qc9_1=. if qc9_1>4
replace qc9_2=. if qc9_2>4
replace qc9_3=. if qc9_3>4
replace qc9_4=. if qc9_4>4
replace qc9_5=. if qc9_5>4
replace qc9_6=. if qc9_6>4
replace qc9_7=. if qc9_7>4
replace qc9_8=. if qc9_8>4
replace qc9_9=. if qc9_9>4
replace qc9_10=. if qc9_11>4

*Notice that for homotransphobia we have specific proxy for disaggregated sexual minorities

g D_sex=((qc9_5+ qc9_10+ qc9_11)-3)/18 if Minority_sex==0
g D_disable=(qc9_9-1)/3 if Minority_disable==0
g D_age=(qc9_6-1)/3 if Age<51


collapse PD_age  PD_sex PD_disable  D_age D_disable   D_sex, by(isocntry)

rename isocntry country
* Take off major outliers among minority groups, determining 0-1 scores due to extremely small number of respondents
drop if PD_sex==0 | PD_disable==0 | PD_age==0
drop if PD_sex==1 | PD_disable==1 | PD_age==1
*cuts only 2 outliers out of 174 total

save Eurobarometer.dta, replace

***************************************************************
				***** LATINOBAROMETRO *****
***************************************************************
*used for Figure2d

*The original dataset can be freely downloaded at https://www.latinobarometro.org/latContents.jsp. You should download the 2009 and 2010 versions


*Rename the downloaded datasets as 2009 and 2010

clear all
use 2009.dta

* Generate Minority variable if respondent is Asian,  Black or Indigenous
g Minority=0
replace Minority=1 if s18==1 | s18==2 | s18==3
replace Minority=. if s18<0

*Generate discriminatory attitudes
replace p34stm_f =. if p34stm_f <0
g D=(p34stm_f-1)/3
replace D=. if Minority==1

*Generate perceptions of discrimination

replace p65n=. if p65n<0
g PD=0 if p65n==2
replace PD=1 if p65n==1
replace PD=. if Minority==0
save  2009.dta, replace

*Repeat for 2010 dataset - slightly different original variables here
clear all
use 2010.dta

g Minority=0
replace Minority=1 if S20==1 | S20==2 | S20==3
replace Minority=. if S20<0
replace P27ST_F =. if P27ST_F <0
g D=(P27ST_F-1 )/3
replace D=. if Minority==1
replace P52ST=. if P52ST<0
g PD=0 if P52ST==2
replace PD=1 if P52ST==1
replace PD=. if Minority==0

*Now append the two

append using 2009.dta

*renaming
rename idenpa Country 
decode Country, gen(country) 
rename numinves round

*collapse 
collapse D PD, by(country round )

*No outliers here due large size of minority

save Latinobarometro.dta,replace


***************************************************************
				***** PEW RESEARCH CENTER ****
***************************************************************
*used for Figure2e

*The original dataset is the 2016 Racial Attitudes in America survey, and can be found at https://www.pewsocialtrends.org/dataset/2016-racial-attitudes-in-america-survey/ 
*Requires registration 

clear all
*The dataset comes in .sav format for spss. After renaming into Pew2016, run the following to turn into stata format.
import spss using "Pew2016.sav", case(lower)
save Pew.dta, replace

clear all
use Pew.dta

*generate  minority variable through race 
 g Black=0  if racethn==1
 replace Black=1 if racethn==2  
 replace Black=. if racethn==9

 *generate perceptions of  discrimination using q27
 * recode don't knows/refuse
  replace q27=. if q27==9
  
  g PD=(4-q27)/3  if Black==1
  g D=(q24a-1)/2 if Black==0


 collapse Black D PD, by(sstate)

rename sstate Sstate
 
decode Sstate, gen(sstate) 

*drop 5 outliers 
drop if PD==0 | PD==1
 
save Pew.dta, replace

***************************************************************
				***** SARI ***** 
***************************************************************
 
* used for Figure2f
* Based on Social Attitudes Research, India, download dataset freely at https://riceinstitute.org/data/social-attitudes-research-india-sari-data/
 
*there are 7 datasets, one for each area. First step is appendig

 use "Bihar.dta", clear
 append using "Dehli.dta"
 append using "Maharashtra.dta"
 append using "Mumbai.dta"
 append using "Rajasthan.dta"
 append using "UttarPradesh"
 
 
 *group status
	gen Muslim=0
	replace Muslim=1 if religion==2

*Main DV
    gen D= nointrelmrg_h & Muslim==0

	gen PD=0 if rlgdisc_5yrs==1
	replace PD=1 if rlgdisc_5yrs==2
	replace PD=1 if rlgdisc_5yrs==3
    replace PD=trtbad_mus_p if PD==.
    replace PD=trtbad_mus_h if PD==.

	* renaming
	
	rename state_res State

	la def ss 1 "Bihar" 2 "Jharkhand" 3 "Dehli" 4 "Maharashtra" 5 "Mumbai" 6 "Rajasthan" 7 "Uttar Pradesh"
	la val State ss

replace type_of_place=4 if  type_of_place==. & State==3
replace type_of_place=1 if  type_of_place==. & State==5 &  area_of_mumbai==1
replace type_of_place=2 if  type_of_place==. & State==5 &  area_of_mumbai==2
replace type_of_place=3 if  type_of_place==. & State==5 &  area_of_mumbai==3
rename  type_of_place Area
*for graphical purposes
drop if State==.

collapse  D PD, by(State Area)
drop if D==0
	
save SARI.dta

*************************************************************** 
				***** AFROBAROMETER *****
***************************************************************

*used for Figure2g
*Based on Afrobarometer 2018, see appendix. Original dataset: https://www.afrobarometer.org/data, free download

*original dataset is .sav
clear all
import spss using "r7_merged_data_34ctry.release.sav", case(lower)
save Afrobarometer.dta, replace

g gender=. if q101==.
replace gender=0 if q101==1
replace gender=1 if q101==2
la var gender "Female"

*generate Main DV
replace q86a=. if q86a==-1
replace q86a=. if q86a>5
g PD=q86a/3 if gender==1

replace q38f=. if q38f>5
g D=(q38f-1)/4 if gender==0
 
collapse D PD, by(country)


save Afrobarometer.dta, replace

***************************************************************
				***** General Social Survey *****
***************************************************************
*used for figure 2h
*Based on general social survey, download here: https://gss.norc.org/

clear all
clear matrix
set maxvar 10000
use "GSS.dta", clear

* Proxies for PD available in 2002 2006 2010 2014 2018, hence
keep if year==2002 | year==2006 | year==2010 | year==2014 | year==2018   

*Main DV
g PD=2-wksexism if sex ==2
g D=(4-fefam)/3 if sex ==1

collapse PD D, by(region year)

drop if PD==0| PD==1

save GSS.dta, replace


***************************************************************
				***** World Value Survey *****
***************************************************************

*used for figure 2i
*Based on World Value Survey

*same steps as before
 
 g Female=0 if V235==1
 replace Female=1 if V235==2
  
 rename V2  country 
  
 *Main DV
 
 g PD=0
 replace PD=1 if V168==2
 replace PD=1 if V169==2
 replace PD=. if  Female==0
 replace PD=. if  Female==.
 
 
 g D=0 if I_WOMJOB==1
 replace D=0.5 if  I_WOMJOB==0.5 | I_WOMJOB==0.500000
 replace D=1 if I_WOMJOB==0
 replace D=. if Female==1
 replace D=. if Female==.
 
 collapse D PD, by(country)
 
*Outliers check - none
 drop if PD==0|PD==1
 
 save WVS.dta, replace


